package org.yagnus.scalasupport.iterates

/**
 * This class delays the creation of an iterator until the last possible moment. The class assumes that
 * the iterable passed to it is never empty.
 *
 * TODO: Check to see if BuffereIterator can be used instead.
 */

object DelayedIterator {
  class DelayedIterator[A](incomming: ⇒ Iterable[A]) extends scala.collection.Iterator[A] {

    var actualItr: Iterator[A] = null;

    def getItr: Iterator[A] = {
      if (actualItr == null) {
        actualItr = incomming.iterator;
      }
      actualItr;
    }

    def hasNext: Boolean = (actualItr == null || actualItr.hasNext);
    def next: A = getItr.next;

    final def delayed = this;
    final def retarded = this;

  }

  implicit def retardTheIterableExecution[Q](in: Iterable[Q]) = new DelayedIterator[Q](in);
}